﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skybot.Tong;
using TygModel;

public partial class file_SiteMaps_Default : System.Web.UI.Page
{
    /// <summary>
    /// 是不是创建HTML
    /// </summary>
    public bool IsCresteHTMLPage = false;

    /// <summary>
    /// 最大的页码
    /// </summary>
    int PageMax = 6;
    string CurrentIndex = "1";
    /// <summary>
    /// 网站通用方法 
    /// </summary>
    public TongUse textData = new TongUse();
    /// <summary>
    /// 数据库访问对像
    /// </summary>
    TygModel.Entities Tygdb = new Entities();

    protected void Page_Load(object sender, EventArgs e)
    {
        bool.TryParse(Request["html"], out  IsCresteHTMLPage);

        CurrentIndex = Request["p"];
        if (string.IsNullOrEmpty(CurrentIndex))
        {
            CurrentIndex = "1";
        }
        Skybot.Tong.TongUse tong = new Skybot.Tong.TongUse();
        //查询数据
        var myDs = Tygdb.书名表.Where(p => p.包含有效章节.Value > 0).OrderBy(p => p.ID).Skip(
            (int.Parse(CurrentIndex) - 1) * 10000
            ).Take(10000);
            //tong.SqlataSet(@"SELECT top " + (int.Parse(CurrentIndex) * 10000) + " id,车辆标识 FROM books");
        DataList1.DataSource = myDs;
        DataList1.DataBind();
        //分页
        pages.DataSource = System.Linq.Enumerable.Range(1, PageMax).Select(x => new { p = x });
        pages.DataBind();

        ///自动生成所有目录
        if (!string.IsNullOrEmpty(Request["c"]))
        {
            foreach (var i in System.Linq.Enumerable.Range(1, PageMax))
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                string baseUrl = Request.Url.AbsoluteUri.Replace(
                    Request.Url.PathAndQuery, Request.Url.LocalPath
                    );

                wc.DownloadString(baseUrl + "?html=true&p=" + i);
                wc.Dispose();
            }
        }

    }

    /// <summary>
    /// 输出HTML
    /// </summary>
    /// <param name="writer"></param>
    protected override void Render(HtmlTextWriter writer)
    {

        //将当前产生的文件写到HTML里
        System.IO.StringWriter sw = new System.IO.StringWriter();
        Html32TextWriter hw = new Html32TextWriter(sw);
        base.Render(hw);
        if (IsCresteHTMLPage)
        {
            //写入数据
            System.IO.File.WriteAllText(Server.MapPath("./" + CurrentIndex + ".html"), sw.ToString());

        }
        //输出页面
        writer.Write(sw.ToString());

        sw.Dispose();
        hw.Dispose();

    }


    /// <summary>
    /// 内容状态保存时发生
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void Page_SaveStateComplete(object sender, EventArgs e)
    {
        //释放资源
        Tygdb.Dispose();
    }

}


    
